#!/usr/bin/perl -w
use strict;
use lib "/share/tools/lg";
use Getopt::Long;
use my_wget;
#88888888888888888888888888888888888888888888888888888888888888888888888888888888

#	Usage

#88888888888888888888888888888888888888888888888888888888888888888888888888888888

my $usage = <<'USAGE';

USAGE:

    bootstrap.pl
				[--download]
				[--recreate_db]
				[--verbose]
		--recreate_db whether panda should be created from scratch
        --from_step specifies the point to start running to
        --to_step specifies the last command to run
USAGE


#88888888888888888888888888888888888888888888888888888888888888888888888888888888888888888

#	Get options

#88888888888888888888888888888888888888888888888888888888888888888888888888888888888888888
# mandatory


# optional parameters
my $recreate_db;
my $download;
my $verbose;
my $help;
GetOptions(
			'download'			=> \$download,
			'recreate_db'		=> \$recreate_db,
			'verbose'			=> \$verbose,
			'help'				=> \$help);

die $usage if ($help);
#die "\nERROR:\n\tMissing Argument: obsolete_db\n" .$usage
#				unless ($pubmed_ids);

#88888888888888888888888888888888888888888888888888888888888888888888888888888888888888888

#	Directory locations

#88888888888888888888888888888888888888888888888888888888888888888888888888888888888888888

my $src_db_root			= '/share/databases';
#my $src_db_root		= '/share/databases';

my $db_name				= 'panda';
my $db_user				= 'postgres';
my $my_dir				= $0;	$my_dir =~ s /(.*)\/.*/$1/;
my $exec_root			= "$my_dir/..";
my $perl_exec_root		= $my_dir;
my $log_root			= "$my_dir/log";
my $exec_tmp			= "$my_dir/panda_tmp";
my $taxa_root			= "$src_db_root/ncbi/taxa";
my $nr_root				= "$src_db_root/ncbi/nr";
my $sprot_root			= "$src_db_root/sprot";


sub get_date_from_time($)
{
	my ($yr,$mon,$day) = (gmtime($_[0]))[5,4,3];
	return (($yr+1900)."_".($mon+1)."_".$day);
}
#88888888888888888888888888888888888888888888888888888888888888888888888888888888888888888

#	Meat

#88888888888888888888888888888888888888888888888888888888888888888888888888888888888888888
# for long commands
my ($cmd1, $cmd2);




if ($recreate_db)
{
print <<"STEP" if $verbose;
#_________________________________________________________________________________________
#	Create panda database
#_________________________________________________________________________________________
STEP
	system("$perl_exec_root/panda_recreate_db.pl -d \"$db_name\" -u \"$db_user\" -clear -all\n");
}





if ($download)
{

print <<"STEP" if $verbose;
#_________________________________________________________________________________________
#    Check whether
#                    [taxa/names.dmp]
#                    [taxa/gi_taxid_prot.dmp]
#                                       are up to date otherwise download
#_________________________________________________________________________________________
STEP
# names.dmp
	{
		my $check_date		= (stat("/share/databases/ncbi/taxa/names.dmp"))[9];
		my $local_path_temp_file = '/share/databases/ncbi/taxa/taxdump.tar.gz';
		my $ret = ftp_get_file_if_newer('ftp.ncbi.nih.gov/pub/taxonomy/taxdump.tar.gz',
								$check_date,
								$local_path_temp_file, $verbose);
		die "$ret\n" unless ($ret >= 1);
		if ($ret == 1)
		{
			print STDERR "Unzipping files...\n";
			die "Error:\nFailed to unzip $local_path_temp_file\n"
				unless (!system("tar -C /share/databases/ncbi/taxa/ -xvzmf $local_path_temp_file"));
			unlink($local_path_temp_file);
		}
		else
		{
			print "No download necessary for names.dmp\n" if $verbose;
		}
	}
# gi_taxid_prot.dmp
	{
		my $check_date		= (stat("/share/databases/ncbi/taxa/gi_taxid_prot.dmp"))[9];
		my $local_path_temp_file = '/share/databases/ncbi/taxa/gi_taxid_prot.dmp.gz';
		my $ret = ftp_get_file_if_newer('ftp.ncbi.nih.gov/pub/taxonomy/gi_taxid_prot.dmp.gz',
								$check_date,
								$local_path_temp_file, $verbose);
		die "$ret\n" unless ($ret >= 1);
		if ($ret == 1)
		{
			die "Error:\nFailed to unzip $local_path_temp_file\n"
				unless (!system("gunzip -f $local_path_temp_file"));
		}
		else
		{
			print "No download necessary for gi_taxid_prot.dmp\n" if $verbose;
		}
	}
}


{	
print <<"STEP";
#_________________________________________________________________________________________
#	Load taxonomy
#_________________________________________________________________________________________
STEP
	($cmd1 = <<"    END") =~ s/^\s+(.*)\n/$1 /gm;
		./panda_load_taxa.pl
			--id_nodes $taxa_root/nodes.dmp
			--names    $taxa_root/names.dmp
			--dbname   $db_name
			--user     $db_user
    END
	print $cmd1, "\n" if $verbose;
	system($cmd1);
}

